package nuc.ss.wl.dao.impl;

import nuc.ss.wl.dao.CourseDao;
import nuc.ss.wl.entity.Class;
import nuc.ss.wl.entity.Course;
import nuc.ss.wl.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoImpl implements CourseDao {
    @Override
    public int save(Course course) {
        String sql = "insert into course values(?,?,?,?)";
        Object[] params = {
                course.getCno(),
                course.getCname(),
                course.getCpno(),
                course.getCcredit()
        };
        return DBUtil.executeUpdate(sql,params);
    }

    @Override
    public List<Course> findAll() {
        //b声明jdbc变量
        Connection conn = null;
        PreparedStatement ps=null;
        ResultSet rs = null;

        //声明变量
        List<Course> list = null;
        try {
            //创建连接
            conn = DBUtil.getConnection();
            String sql = "select * from course";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            list = new ArrayList<Course>();
            while(rs.next()){
                int cno = rs.getInt(1);
                String cname = rs.getString(2);
                int cpno = rs.getInt(3);
                int ccredit = rs.getInt(4);
                Course course = new Course();
                course.setCno(cno);
                course.setCname(cname);
                course.setCpno(cpno);
                course.setCcredit(ccredit);

                list.add(course);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.closeAll(rs,ps,conn);
        }
        return list;
    }

    @Override
    public int delete(int cno) {
        String sql = "delete from course where cno = ?";
        Object[] params = {cno};
        return DBUtil.executeUpdate(sql,params);
    }

    @Override
    public Course findById(int cno) {
        //声明jdbc变量
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        //声明变量
        Course course= null;
        try{
            conn = DBUtil.getConnection();
            String sql = "select * from course where cno = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,cno);
            rs = ps.executeQuery();
            while(rs.next()){
                String cname = rs.getString(2);
                int cpno = rs.getInt(3);
                int ccredit = rs.getInt(4);
                course = new Course();
                course.setCno(cno);
                course.setCname(cname);
                course.setCpno(cpno);
                course.setCcredit(ccredit);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.closeAll(rs,ps,conn);
        }
        return course;
    }

    @Override
    public Course findByCname(String cname) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Course course= null;
        try{
            conn = DBUtil.getConnection();
            String sql = "select * from course where cname = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, cname);
            rs = ps.executeQuery();
            while(rs.next()){
                int cno = rs.getInt(1);
                int cpno = rs.getInt(3);
                int ccredit = rs.getInt(4);
                course = new Course();
                course.setCno(cno);
                course.setCname(cname);
                course.setCpno(cpno);
                course.setCcredit(ccredit);
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs,ps,conn);
        }
        return course;
    }

    @Override
    public int update(Course course) {
        String sql = "update course set cname = ?,cpno=?,ccredit=? where cno = ?";
        Object[] params = {
                course.getCname(),
                course.getCpno(),
                course.getCcredit(),
                course.getCno()
        };
        return DBUtil.executeUpdate(sql,params);
    }
}
